package com.bridge.common.utils;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.annotation.IdType;
import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;

import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

/**
 * MyBatis-Plus 的代码生成器
 *
 * @author Myles Yang
 */
public class CodeAutoUtil {
	public final static String classPath;

	/**
	 * 获取的是classpath路径，适用于读取resources下资源
	 */
	static {
		classPath = Thread.currentThread().getContextClassLoader().getResource("").getPath();
	}
	/**
	 * 获取根路径，并去掉指定层数
	 */
	public static String getRootPath(String str, int back) {
		for (int i = 0; i < back; i++) {
			str = str.substring(0, str.lastIndexOf("/"));
		}
		return str + "/";
	}

	/**
	 * <p>
	 * 读取控制台内容
	 * </p>
	 */
	public static String scanner(String tip) {
		Scanner scanner = new Scanner(System.in);
		System.out.println("请输入" + tip + "：");
		if (scanner.hasNext()) {
			String ipt = scanner.next();
			if (StringUtils.isNotBlank(ipt)) {
				return ipt;
			}
		}
		throw new MybatisPlusException("请输入正确的" + tip + "！");
	}

	public static void main(String[] args) {
        String moduleName = "applets";
        String[] tableNamesToGenerate = {
				"t_diameter",
		};
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 项目根目录
        String projectPath = getRootPath(classPath,4)+moduleName+"-service";

        //// 全局配置
        GlobalConfig gc = new GlobalConfig();
        gc.setAuthor("wenxinji")
                .setOutputDir(projectPath + "/src/main/java")
                .setSwagger2(true)
                // id生成策略
                .setIdType(IdType.AUTO)
                .setOpen(false);
        gc.setEntityName("%sEntity")
				.setControllerName("%sController")
				.setMapperName("%sMapper")
				.setXmlName("%sMapper")
				.setServiceName("%sService")
				.setServiceImplName("%sServiceImpl");
		mpg.setGlobalConfig(gc);

		//// 数据源配置
		DataSourceConfig dsc = new DataSourceConfig();
		dsc.setDbType(DbType.MYSQL)
				.setDriverName("com.mysql.cj.jdbc.Driver")
				.setUrl("jdbc:mysql://1.15.44.254:3306/rebar?useSSL=true&useUnicode=true"
						+ "&characterEncoding=UTF8&serverTimezone=Asia/Shanghai")
				.setUsername("root")
				.setPassword("jkiwervfdgcfLyAlYJCPn");
		mpg.setDataSource(dsc);


		//// 包配置
		PackageConfig pc = new PackageConfig();
		pc.setModuleName(moduleName)
				.setParent("com.rebar")
				.setEntity("model.entity")
				.setController("controller")
				.setMapper("mapper")
				.setXml("mapper.xml")
				.setService("service")
				.setServiceImpl("service.impl");
		mpg.setPackageInfo(pc);


		// 自动填充字段
		List<TableFill> tableFillList = Arrays.asList(
				new TableFill("create_time", FieldFill.INSERT)
                , new TableFill("update_time", FieldFill.INSERT_UPDATE)
                , new TableFill("create_user", FieldFill.INSERT)
                , new TableFill("update_user", FieldFill.INSERT_UPDATE)
        );

		//// 策略配置
		StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel)
                .setColumnNaming(NamingStrategy.underline_to_camel)
                .setEntityLombokModel(true)
                .setRestControllerStyle(true)
                // 字段自动填充
                .setTableFillList(tableFillList)
                .setTablePrefix("t_")
                // 逻辑删除字段名
                .setLogicDeleteFieldName("delete_state")
				// 生成字段注解
				.setEntityTableFieldAnnotationEnable(true)
				// 需要生成的表的名字
				.setInclude(tableNamesToGenerate)
				// 驼峰转连字符：@RequestMapping("/addUser") -> @RequestMapping("/add-user")
				.setControllerMappingHyphenStyle(true);

		mpg.setStrategy(strategy);

		mpg.execute();
	}
}
